package rpcserver

import (
	rpcv1 "gluenet/internal/agent/api/rpc/v1"
	"gluenet/internal/agent/depend"
	"gluenet/internal/agent/driver/rpcserver"
	"gluenet/internal/agent/service/cprofilesvc"
	"gluenet/utils/rpc/v2/define"
)

const (
	cprofile = rpcv1.RPC_SERVICE_CPROFILE
)

func init() {
	depend.Provide(NewCprofileRouter)
	registerRouter(cprofile, func(c rpcserver.IRoutes) {
		depend.Invoke(func(d *cprofileRouter) {
			d.initRouter(c)
		})
	})
}

type cprofileRouter struct {
	cprofile *cprofilesvc.Service
}

func NewCprofileRouter(svc *cprofilesvc.Service) *cprofileRouter {
	return &cprofileRouter{svc}
}

func (r *cprofileRouter) initRouter(c rpcserver.IRoutes) {
	c.Group("cprofile").
		Func(`flame`, r.cprofileFlame).
		Func(`file`, r.cprofileFile)
}

func (r *cprofileRouter) cprofileFlame(c *rpcserver.Context) {
	cprofileFlameReq := new(define.CprofileFlame)
	if err := c.BindJSON(&struct {
		Spec interface{} `json:"spec,omitempty"`
	}{cprofileFlameReq}); err != nil {
		return
	}
	resp := r.cprofile.CprofileFlame(*cprofileFlameReq)
	c.JSON(resp)
}

func (r *cprofileRouter) cprofileFile(c *rpcserver.Context) {
	cprofileFileReq := new(define.CprofileFile)
	if err := c.BindJSON(&struct {
		Spec interface{} `json:"spec,omitempty"`
	}{cprofileFileReq}); err != nil {
		return
	}
	resp := r.cprofile.CprofileFile(*cprofileFileReq)
	c.JSON(resp)
}
